package com.manner.groupon.controller.user;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.model.entity.Menu;
import com.manner.groupon.service.menu.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: ywj
 * @Description: 快捷菜单相关业务请求控制器
 * @DateTime: 2024/10/10 12:02
 */
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
    
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping
    public BaseResponse<List<Menu>> list(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false,defaultValue = "0") Integer action) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        //判断
        if (!action.equals(1)) {
            wrapper.eq("is_action", action);
        }
        if (!StrUtil.isNullOrUndefined(name)) {
            wrapper.like("name", '%' + name + '%');
        }
        wrapper.orderByAsc("sort");
        //查询
        List<Menu> list = menuService.list(wrapper);
        return ResultUtils.success(list);
    }

    @GetMapping("/{id}")
    public BaseResponse<Menu> queryById(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        return ResultUtils.success(menu);
    }

    @PostMapping
    public BaseResponse<String> save(@RequestBody Menu menu) {
        menuService.save(menu);
        return ResultUtils.success("保存成功");
    }

    @PutMapping
    public BaseResponse<String> update(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return ResultUtils.success("修改成功");
    }

    @DeleteMapping("/{id}")
    public BaseResponse<String> del(@PathVariable Long id) {
        menuService.removeById(id);
        return ResultUtils.success("删除成功");
    }
}
